package com.ominext.camera;

import java.io.IOException;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Camera;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

import com.ominext.beautifulcapture.R;
import com.ominext.home.Path;

@SuppressLint("ViewConstructor")
class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

	public SurfaceHolder surfaceHolder;
	public Camera camera;
	private final String TAG = "Preview";
	CameraPreview preview;
	boolean previewing = false;
	Camera.CameraInfo currentCameraId = new Camera.CameraInfo();

	public CameraPreview(Context context, Camera mCamera) {
		super(context);
		camera = mCamera;
		surfaceHolder = getHolder();
		surfaceHolder.addCallback(this);
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}
	
	@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        
        Bitmap icon = BitmapFactory.decodeFile(Path.imageShowPath);
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(icon, 10, 10, new Paint());        
    }

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {

		if (previewing) {
			camera.stopPreview();
			previewing = false;
		}

		if (camera != null) {
			try {
				// camera.setDisplayOrientation(90); // Fix bug Sony lt26w auto
				// rotate camera
				camera.setPreviewDisplay(surfaceHolder);
				camera.startPreview();
				previewing = true;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		try {
			camera.setPreviewDisplay(holder);
			camera.startPreview();
			setCameraDisplayOrientation(currentCameraId.facing, camera); // Fix
																			// bug
																			// auto
																			// rotate
																			// camera
		} catch (IOException e) {
			Log.d(TAG, "Error setting camera preview (IO): " + e.getMessage());
		} catch (RuntimeException e) {
			Log.d(TAG,
					"Error setting camera preview (RunTime): " + e.getMessage());
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		if (camera != null) {
			// this.getHolder().removeCallback(this);
			camera.stopPreview(); // This code make our program crash@@ Not
									// really
			camera.setPreviewCallback(null);
			camera.release();
			camera = null;
			previewing = false;
		}
	}

	public void setCameraDisplayOrientation(int cameraId,
			android.hardware.Camera camera) {
		android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
		android.hardware.Camera.getCameraInfo(cameraId, info);
		int rotation = ((WindowManager) getContext().getSystemService(
				Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
		int degrees = 0;
		switch (rotation) {
		case Surface.ROTATION_0:
			degrees = 0;
			break;
		case Surface.ROTATION_90:
			degrees = 90;
			break;
		case Surface.ROTATION_180:
			degrees = 180;
			break;
		case Surface.ROTATION_270:
			degrees = 270;
			break;
		}

		int result;
		if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
			result = (info.orientation + degrees) % 360;
			result = (360 - result) % 360; // compensate the mirror
		} else { // back-facing
			result = (info.orientation - degrees + 360) % 360;
		}
		// set the right preview orientation
		camera.setDisplayOrientation(result);
		// make the camera output a rotated image
		Camera.Parameters cameraParameters = camera.getParameters();
		if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
			Log.d("Degrees", " " + degrees);
			cameraParameters.setRotation(result + 180);
		} else { // back-facing
			cameraParameters.setRotation(result);
		}
		camera.setParameters(cameraParameters);
	}
}
